#include <stdio.h>
#define MAX 100

void print_menu()
{
	printf("0.EXIT 1.INSERT 2.MAXIMUM 3.EXTRACT_MAX\n");
	printf("please input selection:");
}

void heap_insert(int *arr,int *n)
{
	if(*n == MAX)
	{
		printf("Error,the queue is full.\n");
		return;
	}
	int x;
	scanf("%d",&x);
	arr[*n]=x;
	int s = *n;
	while((s - 1) / 2 >= 0)
	{
		if(arr[s] > arr[(s - 1) / 2])
		{
			int temp = arr[(s - 1) / 2];
			arr[(s - 1) / 2] = arr[s];
			arr[s] = temp;
			s = (s - 1) / 2;
		}
		else
		{
			s = -1;
		}
	}
	(*n)++;
}

void heap_maximum(int *arr,int *n)
{
	if((*n) == 0)
	{
		printf("Error,the queue is empty.\n");
		return;
	}
	printf("%d\n",arr[0]);
}

void heap_exract_max(int *arr,int *n)
{
	if((*n) == 0)
	{
		printf("Error,the queue is empty.\n");
		return;
	}
	int x = 0;
	printf("%d\n",arr[0]);
	while(x < (*n))
	{
		if(arr[x * 2 + 1] > arr[x * 2 + 2])
		{
			arr[x] = arr[x * 2 + 1];
			x = x * 2 + 1;
		}
		else 
		{
			arr[x] = arr[x * 2 + 2];
			x = x * 2 + 2;
		}
	}
	arr[(x - 1) / 2] = arr[--(*n)];
}

int main()
{
	int selection,arr[MAX],n = 0;
	while(1)
	{
		print_menu();
		scanf("%d",&selection);
		switch(selection)
		{
			case 0:
				return 0;
			case 1:
				heap_insert(arr,&n);
				break;
			case 2:
				heap_maximum(arr,&n);
				break;
			case 3:
				heap_exract_max(arr,&n);
				break;
		}
	}
	return 0;
}
